package com.yellowpage.bigdata.action;
  
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import com.yellowpage.bigdata.data.TmApponoffImeiChlRetain;
import com.yellowpage.bigdata.service.TmApponoffImeiChlRetainService;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;

public class TmApponoffImeiChlRetainAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<TmApponoffImeiChlRetain> tmApponoffImeiChlRetainList = new ArrayList<TmApponoffImeiChlRetain>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private TmApponoffImeiChlRetain tmApponoffImeiChlRetain;
	private TmApponoffImeiChlRetainService tmApponoffImeiChlRetainService;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	
		private String query_tmApponoffImeiChlRetainId;
		private String query_imeiNewCount;
		private String query_nextDayRetained;
		private String query_threeDayRetained;
		private String query_sevenDayRetained;
		private String query_thirtyDayRetained;
		private String query_createUser;
		private String query_createTime;
		private String query_channelNo;
		private String query_showDate;
		private String query_logDate;
		private String query_note;
		private String query_showDate_end;
		
	public void prepare() {
		super.prepare();
	
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.tmApponoffImeiChlRetain = tmApponoffImeiChlRetainService.getTmApponoffImeiChlRetain(Long.parseLong(selectedId));
		}
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
		 queryMap.put("showDate", query_showDate);
		 queryMap.put("query_showDate_end", query_showDate_end);
 	     queryMap.put("channelNo", query_channelNo);
 	     

	    	    	
		resultList = tmApponoffImeiChlRetainService.queryTmApponoffImeiChlRetainByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public void exportExcel() {
		Map queryMap = new HashMap();
		queryMap.put("showDate", query_showDate);
		queryMap.put("query_showDate_end", query_showDate_end);
		queryMap.put("channelNo", query_channelNo);
		resultList = tmApponoffImeiChlRetainService
				.queryTmApponoffImeiChlRetainByPage(queryMap);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/x-download");
		OutputStream outp = null;
		response.addHeader("Content-Disposition", "attachment;filename="
				+ "ImeiChlRetain.xls");
		try {
			outp = response.getOutputStream();
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("ImeiChlRetain");
			sheet.autoSizeColumn(1);
			HSSFCellStyle cellStyle = workbook.createCellStyle(); // 单元格格式
			cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); // 设置右边框
			cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
			HSSFRow row1 = sheet.createRow(0);// 二级标题栏
			String[] title = { "日期", "渠道号", "渠道名称 ", "IMEI新增数 ", "次日留存率",
					"第3日留存率", "第7日留存率", "第30日留存率 " };
			for (int i = 0, len = title.length; i < len; i++) {
				HSSFCell cell = row1.createCell(i);
				cell.setCellValue(title[i]);
			}
			for (int i = 0, len = resultList.size(); i < len; i++) {
				HSSFRow data_row = sheet.createRow(i + 1);
				Map result = resultList.get(i);
				data_row.createCell(0).setCellValue(
						result.get("SHOW_DATE") != null ? result.get(
								"SHOW_DATE").toString() : "");
				data_row.createCell(1).setCellValue(
						result.get("CHANNEL_NO") != null ? result.get(
								"CHANNEL_NO").toString() : "");
				data_row.createCell(2).setCellValue(
						result.get("CHANNEL_NO_NAME") != null ? result.get(
								"CHANNEL_NO_NAME").toString() : "");
				data_row.createCell(3).setCellValue(
						result.get("IMEI_NEW_COUNT") != null ? result.get(
								"IMEI_NEW_COUNT").toString() : "");
				data_row.createCell(4).setCellValue(
						result.get("NEXT_RETAINED") != null ? result.get(
								"NEXT_RETAINED").toString() : "");
				data_row.createCell(5).setCellValue(
						result.get("THREE_RETAINED") != null ? result.get(
								"THREE_RETAINED").toString() : "");
				data_row.createCell(6).setCellValue(
						result.get("SEVEN_RETAINED") != null ? result.get(
								"SEVEN_RETAINED").toString() : "");
				data_row.createCell(7).setCellValue(
						result.get("THIRTY_RETAINED") != null ? result.get(
								"THIRTY_RETAINED").toString() : "");
			}
			workbook.write(outp);
			response.flushBuffer();
			outp.flush();
			response.reset();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			this.addActionError("** 生成文件错误 !");
		} finally {
			if (outp != null) {
				try {
					outp.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}
		}
	}
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {
	    
		tmApponoffImeiChlRetainService.saveTmApponoffImeiChlRetain(tmApponoffImeiChlRetain);		
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
	    	    	    	    	    	    	    if (tmApponoffImeiChlRetain.getNextDayRetained().length() == 0) {
			this.addActionError("** NextDayRetained 不能为空 !");
		}
			    	    	    if (tmApponoffImeiChlRetain.getThreeDayRetained().length() == 0) {
			this.addActionError("** ThreeDayRetained 不能为空 !");
		}
			    	    	    if (tmApponoffImeiChlRetain.getSevenDayRetained().length() == 0) {
			this.addActionError("** SevenDayRetained 不能为空 !");
		}
			    	    	    if (tmApponoffImeiChlRetain.getThirtyDayRetained().length() == 0) {
			this.addActionError("** ThirtyDayRetained 不能为空 !");
		}
			    	    	    	    	    	    	    if (tmApponoffImeiChlRetain.getChannelNo().length() == 0) {
			this.addActionError("** ChannelNo 不能为空 !");
		}
			    	    	    if (tmApponoffImeiChlRetain.getShowDate().length() == 0) {
			this.addActionError("** ShowDate 不能为空 !");
		}
			    	    	    if (tmApponoffImeiChlRetain.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
			    	    	    if (tmApponoffImeiChlRetain.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
			    	    
		this.setValidatorResult("/bigdata/TmApponoffImeiChlRetainAdd.jsp");		
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    tmApponoffImeiChlRetain = tmApponoffImeiChlRetainService.getTmApponoffImeiChlRetain(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    tmApponoffImeiChlRetainService.updateTmApponoffImeiChlRetain(tmApponoffImeiChlRetain);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
					    			    			    if (tmApponoffImeiChlRetain.getNextDayRetained().length() == 0) {
			this.addActionError("** NextDayRetained 不能为空 !");
		}
			    			    if (tmApponoffImeiChlRetain.getThreeDayRetained().length() == 0) {
			this.addActionError("** ThreeDayRetained 不能为空 !");
		}
			    			    if (tmApponoffImeiChlRetain.getSevenDayRetained().length() == 0) {
			this.addActionError("** SevenDayRetained 不能为空 !");
		}
			    			    if (tmApponoffImeiChlRetain.getThirtyDayRetained().length() == 0) {
			this.addActionError("** ThirtyDayRetained 不能为空 !");
		}
			    			    			    			    if (tmApponoffImeiChlRetain.getChannelNo().length() == 0) {
			this.addActionError("** ChannelNo 不能为空 !");
		}
			    			    if (tmApponoffImeiChlRetain.getShowDate().length() == 0) {
			this.addActionError("** ShowDate 不能为空 !");
		}
			    			    if (tmApponoffImeiChlRetain.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
			    			    if (tmApponoffImeiChlRetain.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
			    		this.setValidatorResult("/bigdata/TmApponoffImeiChlRetainUpdate.jsp");	
	}
	
	public String delete() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			this.tmApponoffImeiChlRetainService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}
		
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setTmApponoffImeiChlRetain(TmApponoffImeiChlRetain tmApponoffImeiChlRetain) {
		this.tmApponoffImeiChlRetain = tmApponoffImeiChlRetain;
	}
	
	public TmApponoffImeiChlRetain getTmApponoffImeiChlRetain() {
		return tmApponoffImeiChlRetain;
	}
	 
	 
	 public void setTmApponoffImeiChlRetainService(TmApponoffImeiChlRetainService tmApponoffImeiChlRetainService) {
		this.tmApponoffImeiChlRetainService = tmApponoffImeiChlRetainService;
	}
	
	public List<TmApponoffImeiChlRetain> getTmApponoffImeiChlRetainList() {
		return tmApponoffImeiChlRetainList;
	}

	public void setTmApponoffImeiChlRetainList(List<TmApponoffImeiChlRetain> tmApponoffImeiChlRetainList) {
		this.tmApponoffImeiChlRetainList = tmApponoffImeiChlRetainList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_tmApponoffImeiChlRetainId() {
        return this.query_tmApponoffImeiChlRetainId;
    }
    
    public void setQuery_tmApponoffImeiChlRetainId(String tmApponoffImeiChlRetainId){
        this.query_tmApponoffImeiChlRetainId = tmApponoffImeiChlRetainId;
    }    

        public String getQuery_imeiNewCount() {
        return this.query_imeiNewCount;
    }
    
    public void setQuery_imeiNewCount(String imeiNewCount){
        this.query_imeiNewCount = imeiNewCount;
    }    

        public String getQuery_nextDayRetained() {
        return this.query_nextDayRetained;
    }
    
    public void setQuery_nextDayRetained(String nextDayRetained){
        this.query_nextDayRetained = nextDayRetained;
    }    

        public String getQuery_threeDayRetained() {
        return this.query_threeDayRetained;
    }
    
    public void setQuery_threeDayRetained(String threeDayRetained){
        this.query_threeDayRetained = threeDayRetained;
    }    

        public String getQuery_sevenDayRetained() {
        return this.query_sevenDayRetained;
    }
    
    public void setQuery_sevenDayRetained(String sevenDayRetained){
        this.query_sevenDayRetained = sevenDayRetained;
    }    

        public String getQuery_thirtyDayRetained() {
        return this.query_thirtyDayRetained;
    }
    
    public void setQuery_thirtyDayRetained(String thirtyDayRetained){
        this.query_thirtyDayRetained = thirtyDayRetained;
    }    

        public String getQuery_createUser() {
        return this.query_createUser;
    }
    
    public void setQuery_createUser(String createUser){
        this.query_createUser = createUser;
    }    

        public String getQuery_createTime() {
        return this.query_createTime;
    }
    
    public void setQuery_createTime(String createTime){
        this.query_createTime = createTime;
    }    

        public String getQuery_channelNo() {
        return this.query_channelNo;
    }
    
    public void setQuery_channelNo(String channelNo){
        this.query_channelNo = channelNo;
    }    

        public String getQuery_showDate() {
        return this.query_showDate;
    }
    
    public void setQuery_showDate(String showDate){
        this.query_showDate = showDate;
    }    

        public String getQuery_logDate() {
        return this.query_logDate;
    }
    
    public void setQuery_logDate(String logDate){
        this.query_logDate = logDate;
    }

	public String getQuery_showDate_end() {
		return query_showDate_end;
	}

	public void setQuery_showDate_end(String query_showDate_end) {
		this.query_showDate_end = query_showDate_end;
	}      

        public String getQuery_note() {
        return this.query_note;
    }
    
    public void setQuery_note(String note){
        this.query_note = note;
    }    

    
	
}
